package praktikumid.p03_k;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Kolmas praks. exception jms.
 * @author ago
 *
 */
public class Kolmas {

	/**
	 * main.
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("argumentide arv:" + args.length);
		for (int i = 0; i < args.length; i++) {
			System.out.println(args[i]);
		}
		//System.out.println(args[10]);
		try {
			System.out.println("siin on kood");
			// kood
			// tekib viga -> catch osasse
			args[10] = "tere";
			// siia ei jouta
			System.out.println("siin on kood");
		} catch (Exception e) {
			// kaivitub catch
			System.out.println("tekkis viga" +
					e.getMessage());
		}
		/*
		String line1 = readLine();
		System.out.println("sain rea: " + line1);

		String line2;
		try {
			line2 = readLine2();
		} catch (IOException e) {
			e.printStackTrace();
			line2 = "";
		}
		System.out.println("sain rea: " + line2);
		*/
		System.out.print("sisesta arv:");
		int i = readInt();
		System.out.println("sain arvu:" + i);
		
		System.out.print("sisesta arv:");
		int j = 0;
		try {
			j = readInt2();
		} catch (NumberFormatException e) {
			System.out.println("Ei ole arv, on 10");
			System.out.println(e.getMessage());
			j = 10;
		} catch (IOException e) {
			System.exit(0);
		}
		System.out.println("sain arvu:" + j);
		
		int k = 0;
		while (true) {
			System.out.print("sisesta arv:");
			try {
				k = readInt2();
				break;
			} catch (NumberFormatException e) {
				System.out.println("ei ole number");
			} catch (IllegalArgumentException e) {
				System.out.println("liiga suur number");
				//System.out.println(e.getMessage());
				System.out.println("Ei ole arv, proovi veel");
			} catch (IOException e) {
				System.exit(0);
			}
			
		}
		System.out.println("sain arvu:" + k);
	}
	
	/**
	 * Loeb rea.
	 * @return loetud rida
	 */
	public static String readLine() {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		
		String line;
		try {
			line = br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
			line = "";
		}
		return line;
	}
	
	/**
	 * Loeb rea.
	 * @return loetud rida
	 * @throws IOException
	 */
	public static String readLine2() throws IOException {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		
		String line;
		line = br.readLine();
		return line;
	}

	
	/**
	 * Loeb taisarvu. Kui ei sisestata taisarvu, tagastab 0.
	 * @return taisarv
	 */
	public static int readInt() {
		try {
			String line = readLine2();
			int i = Integer.parseInt(line);
			return i;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			System.out.println("Ei ole number, tagastame 0");
			return 0;
		}
		return 0;
	}
	
	/**
	 * Loeb taisarvu.
	 * @return 
	 * @throws IOException
	 * @throws NumberFormatException
	 */
	public static int readInt2() throws IOException, NumberFormatException {
		String line = readLine2();
		
		try {
			int i = Integer.parseInt(line);
			if (i > 10) {
				throw new IllegalArgumentException("number on liiga suur");
			} 
			return i;
		} catch (NumberFormatException e) {
			throw new NumberFormatException("Sisestatud rida ei ole number");
		}
	}
}
